home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFskyedit / c / Menus < prev    next >
Encoding:
Text File  |  2003-10-16  |  14.2 KB  |  395 lines

  1. /*
  2.  *  SFskyedit - Star Fighter 3000 sky colours editor
  3.  *  Menu attached to sky window (all levels)
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdbool.h>
  26.  
  27. /* RISC OS library files */
  28. #include "kernel.h"
  29. #include "wimp.h"
  30. #include "toolbox.h"
  31. #include "event.h"
  32. #include "wimplib.h"
  33. #include "menu.h"
  34.  
  35. /* My library files */
  36. #include "err.h"
  37. #include "msgtrans.h"
  38. #include "Macros.h"
  39. #include "Pal256.h"
  40. #include "FilePerc.h"
  41.  
  42. /* Local headers */
  43. #include "EditSky.h"
  44. #include "SFSFileInfo.h"
  45. #include "SFSSaveBox.h"
  46. #include "Interpolate.h"
  47. #include "Insert.h"
  48. #include "Back-end.h"
  49. #include "DCS_dialogue.h"
  50. #include "Utils.h"
  51. #include "Main.h"
  52. #include "Menus.h"
  53.  
  54. /* Menu entries */
  55. #define SKYMENU_FILE      0x01
  56. #define SKYMENU_EDIT      0x02
  57. #define SKYMENU_COLS      0x0a
  58. #define SKYMENU_TRAP      0x09
  59. #define SKYMENU_PREVIEW   0x05
  60. #define SKYMENU_HELP      0x08
  61.  
  62. #define FILEMENU_INFO    0x01
  63. #define FILEMENU_SAVE    0x02
  64. #define FILEMENU_DISCARD 0x00
  65.  
  66. #define EDITMENU_INSERTNEW 0x0b
  67. #define EDITMENU_CUT       0x08
  68. #define EDITMENU_COPY      0x07
  69. #define EDITMENU_PASTE     0x09
  70. #define EDITMENU_DELETE    0x06
  71. #define EDITMENU_SELALL    0x0a
  72. #define EDITMENU_CLEARSEL  0x03
  73.  
  74. #define COLSMENU_SETCOL    0x01
  75. #define COLSMENU_GRADIENT  0x05
  76. #define COLSMENU_SMOOTH    0x04
  77.  
  78. ObjectId SkyMenu_sharedid = NULL_ObjectId, EditMenu_sharedid = NULL_ObjectId, ColsMenu_sharedid = NULL_ObjectId;
  79.  
  80. /* ----------------------------------------------------------------------- */
  81. /*                       Function prototypes                               */
  82.  
  83. static ToolboxEventHandler _SkyMenu_showhandler, _SkyMenu_selectionhandler, _FileMenu_selectionhandler, _EditMenu_selectionhandler, _EditMenu_showhandler, _ColsMenu_colourselhandler, _ColsMenu_selectionhandler, _ColsMenu_showhandler;
  84.  
  85. /* ----------------------------------------------------------------------- */
  86. /*                         Public functions                                */
  87.  
  88. void SkyMenu_initialise(ObjectId id)
  89. {
  90.   SkyMenu_sharedid = id;
  91.  
  92.   /* Install handlers */
  93.   EF(event_register_toolbox_handler(id, Menu_Selection, _SkyMenu_selectionhandler, NULL));
  94.   EF(event_register_toolbox_handler(id, Menu_AboutToBeShown, _SkyMenu_showhandler, NULL));
  95. }
  96.  
  97. /* ----------------------------------------------------------------------- */
  98.  
  99. void FileMenu_initialise(ObjectId id)
  100. {
  101.   /* Install handlers */
  102.   EF(event_register_toolbox_handler(id, Menu_Selection, _FileMenu_selectionhandler, NULL));
  103. }
  104.  
  105. /* ----------------------------------------------------------------------- */
  106.  
  107. void EditMenu_initialise(ObjectId id)
  108. {
  109.   EditMenu_sharedid = id;
  110.  
  111.   /* Install handlers */
  112.   EF(event_register_toolbox_handler(id, Menu_Selection, _EditMenu_selectionhandler, NULL));
  113.   EF(event_register_toolbox_handler(id, Menu_AboutToBeShown, _EditMenu_showhandler, NULL));
  114. }
  115.  
  116. /* ----------------------------------------------------------------------- */
  117.  
  118. void ColsMenu_initialise(ObjectId id)
  119. {
  120.   ColsMenu_sharedid = id;
  121.  
  122.   /* Install handlers */
  123.   EF(event_register_toolbox_handler(id, Menu_Selection, _ColsMenu_selectionhandler, NULL));
  124.   EF(event_register_toolbox_handler(id, Menu_AboutToBeShown, _ColsMenu_showhandler, NULL));
  125.   EF(event_register_toolbox_handler(pal256_sharedid, Pal256_ColourSelected, _ColsMenu_colourselhandler, NULL));
  126. }
  127.  
  128. /* ----------------------------------------------------------------------- */
  129.  
  130. //void SkyMenu_update(ViewData *view_data)
  131. //{
  132. //  RE(menu_set_tick(0, SkyMenu_sharedid, SKYMENU_TRAP, (int)view_data->trap_caret))
  133. //}
  134.  
  135. /* ----------------------------------------------------------------------- */
  136.  
  137. void EditMenu_update(ViewData *view_data)
  138. {
  139.   /* If full selection then prevent select all */
  140.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_SELALL, (view_data->selection_exists && view_data->selection_start == 0 && view_data->selection_end == 62)))
  141.  
  142.   /* Prevent operations on selection if none */
  143.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_CLEARSEL, !view_data->selection_exists))
  144.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_CUT, !view_data->selection_exists))
  145.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_COPY, !view_data->selection_exists))
  146.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_DELETE, !view_data->selection_exists))
  147.   
  148.   /* Prevent insertion at caret if none */
  149.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_INSERTNEW, view_data->selection_exists))
  150.   
  151.   /* Prevent paste if no clipboard contents */
  152.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_PASTE, clipboard == NULL))
  153. }
  154.  
  155.  
  156. /* ----------------------------------------------------------------------- */
  157.  
  158. //void ColsMenu_update(ViewData *view_data)
  159. //{
  160. //  /* Prevent operations if none selected (or not enough bands) */
  161. //  RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_SETCOL, !view_data->selection_exists))
  162. //  RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_GRADIENT, !view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 2))
  163. //  RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_SMOOTH, !view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 3))
  164. //}
  165.  
  166. /* ----------------------------------------------------------------------- */
  167. /*                         Private functions                               */
  168.  
  169. static int _SkyMenu_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  170. {
  171.   ViewData *view_data;
  172.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
  173.   
  174.   //SkyMenu_update(view_data);
  175.   RE(menu_set_tick(0, SkyMenu_sharedid, SKYMENU_TRAP, (int)view_data->trap_caret))
  176.  
  177.   return 0; /* claim event */
  178. }
  179.  
  180. /* ----------------------------------------------------------------------- */
  181.  
  182. static int _SkyMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  183. {
  184.   ViewData *view_data;
  185.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
  186.  
  187.   switch(id_block->self_component) {
  188.  
  189.     case SKYMENU_PREVIEW:
  190.       EditSky_preview(view_data);
  191.       return 1; /* claim event */
  192.       
  193.     case SKYMENU_TRAP:
  194.       view_data->trap_caret = !view_data->trap_caret;
  195.       def_trap_caret = view_data->trap_caret;
  196.       RE(menu_set_tick(0, SkyMenu_sharedid, SKYMENU_TRAP, (int)view_data->trap_caret))
  197.       return 1; /* claim event */
  198.       
  199.     default:
  200.       return 0; /* event not handled */
  201.   }
  202. }
  203.  
  204. /* ----------------------------------------------------------------------- */
  205.  
  206. static int _FileMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  207. {
  208.   ViewData *view_data;
  209.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
  210.  
  211.   switch(id_block->self_component) {
  212.     case FILEMENU_INFO:
  213.       RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, fileinfo_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
  214.       return 1; /* claim event */
  215.  
  216.     case FILEMENU_SAVE:
  217.       if(strchr(view_data->last_savepath, (int)'.') == NULL) {
  218.         /* Must open savebox first */
  219.         RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, savebox_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
  220.       }
  221.       else {
  222.         /* Save file immediately */
  223.         _kernel_oserror *err = perc_operation(FILEPERC_OP_COMP, view_data->last_savepath, FILETYPE_SKYCOLS, (flex_ptr)&view_data->sky);
  224.         if(err != NULL)
  225.           /* Saving error */
  226.           err_report(err->errnum, msgs_lookup_sub1("SaveFail", err->errmess));
  227.         else
  228.           EditSky_newfile(view_data, view_data->last_savepath, 1);
  229.       }
  230.       return 1; /* claim event */
  231.  
  232.     case FILEMENU_DISCARD:
  233.       if(view_data->changed_since_save) {
  234.         /* Unsaved data - prompt user for action */
  235.         dcs_openparent = false;
  236.         RE(open_topleftofwin(Toolbox_ShowObject_AsMenu, dcs_sharedid, id_block->ancestor_id, id_block->self_id, id_block->self_component))
  237.       }
  238.       else {
  239.         /* Close immediately */
  240.         RE(toolbox_delete_object(0, id_block->ancestor_id))
  241.       }
  242.       return 1; /* claim event */
  243.   }
  244.  
  245.   return 0; /* event not handled */
  246. }
  247.  
  248. /* ----------------------------------------------------------------------- */
  249.  
  250. static int _EditMenu_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  251. {
  252.   ViewData *view_data;
  253.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
  254.   EditMenu_update(view_data);
  255.   return 1; /* claim event */
  256. }
  257.  
  258. /* ----------------------------------------------------------------------- */
  259.  
  260. static int _EditMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  261. {
  262.   ViewData *view_data;
  263.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
  264.  
  265.   switch(id_block->self_component) {
  266.     case EDITMENU_PASTE:
  267.       /* Insert rows from clipboard */
  268.       EditSky_paste(view_data);
  269.       break;
  270.  
  271.     case EDITMENU_INSERTNEW:
  272.       if(view_data->selection_exists) {
  273.         _kernel_oswrch(7); /* beep */
  274.         return 1;
  275.       }
  276.       RE(wimp_create_menu(CloseMenu,0,0))
  277.       RE(toolbox_show_object(0, Insert_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
  278.       return 1; /* don't update menu */
  279.  
  280.     case EDITMENU_COPY:
  281.       if(!view_data->selection_exists) {
  282.         _kernel_oswrch(7); /* beep */
  283.         return 1;
  284.       }
  285.       copy_to_clipboard(&view_data->sky, view_data->selection_start, view_data->selection_end);
  286.       break;
  287.  
  288.     case EDITMENU_CUT:
  289.       if(!view_data->selection_exists) {
  290.         _kernel_oswrch(7); /* beep */
  291.         return 1;
  292.       }
  293.       if(!copy_to_clipboard(&view_data->sky, view_data->selection_start, view_data->selection_end))
  294.         return 1;
  295.       EditSky_remove_sel(view_data);
  296.       break;
  297.  
  298.     case EDITMENU_DELETE:
  299.       if(!view_data->selection_exists) {
  300.         _kernel_oswrch(7); /* beep */
  301.         return 1;
  302.       }
  303.       EditSky_remove_sel(view_data);
  304.       break;
  305.      
  306.     case EDITMENU_SELALL:
  307.       EditSky_changeselection(view_data, 0, 62);
  308.       break;
  309.  
  310.     case EDITMENU_CLEARSEL:
  311.       EditSky_clearselection(view_data);
  312.       break;
  313.       
  314.     default:
  315.       return 0; /* event not handled */
  316.   }
  317.   
  318.   /* Update menu state (selection and/or clipboard status may have changed) */
  319.   EditMenu_update(view_data);
  320.   
  321.   return 1; /* claim event */
  322. }
  323.  
  324. /* ----------------------------------------------------------------------- */
  325.  
  326. static int _ColsMenu_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  327. {
  328.   ViewData *view_data;
  329.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
  330.   //ColsMenu_update(view_data);
  331.   
  332.   /* Prevent operations if none selected (or not enough bands) */
  333.   RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_SETCOL, !view_data->selection_exists))
  334.   RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_GRADIENT, !view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 2))
  335.   RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_SMOOTH, !view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 3))
  336.  
  337.   return 1; /* claim event */
  338. }
  339.  
  340. /* ----------------------------------------------------------------------- */
  341.  
  342. static int _ColsMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  343. {
  344.   ViewData *view_data;
  345.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
  346.  
  347.   switch(id_block->self_component) {
  348.     case COLSMENU_SETCOL:
  349.       if(!view_data->selection_exists) {
  350.         _kernel_oswrch(7); /* beep */
  351.         return 1; /* ignore if no selection at present */
  352.       }
  353.       if(!E(Pal256_set_colour(pal256_sharedid, get_shade(&view_data->sky, view_data->selection_start))))
  354.         RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, pal256_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
  355.       return 1; /* claim event */
  356.  
  357.     case COLSMENU_SMOOTH:
  358.       if(!view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 3) {
  359.         _kernel_oswrch(7); /* beep */
  360.         return 1; /* ignore if no selection at present */
  361.       }
  362.       smooth_section(&view_data->sky, view_data->selection_start, view_data->selection_end);
  363.       EditSky_rowsupdated(view_data, view_data->selection_start, view_data->selection_end);
  364.       EditSky_markaschanged(view_data);
  365.       return 1; /* claim event */
  366.  
  367.     case COLSMENU_GRADIENT:
  368.       if(!view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 2) {
  369.         _kernel_oswrch(7); /* beep */
  370.         return 1; /* ignore if no selection at present */
  371.       }
  372.       RE(wimp_create_menu(CloseMenu,0,0))
  373.       RE(toolbox_show_object(0, Interpolate_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
  374.       return 1; /* claim event */
  375.   }
  376.   return 0; /* event not handled */
  377. }
  378.  
  379. /* ----------------------------------------------------------------------- */
  380.  
  381. static int _ColsMenu_colourselhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  382. {
  383.   /* Colour selected - fill selected skyfile bands */
  384.   if(id_block->parent_id != ColsMenu_sharedid)
  385.     return 0; /* none of our business */
  386.  
  387.   ViewData *view_data;
  388.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
  389.   plain_overwrite(&view_data->sky, view_data->selection_start, view_data->selection_end, ((Pal256ColourSelectedEvent *)event)->colour_number);
  390.   EditSky_rowsupdated(view_data, view_data->selection_start, view_data->selection_end);
  391.   EditSky_markaschanged(view_data);
  392.  
  393.   return 1; /* claim event */
  394. }
  395.